1764D - Doremy's Pegging Game - CodeForces Solution


combinatorics

Please click on ads to support us..

Python Code:

import io,os
input = io.BytesIO(os.read(0, os.fstat(0).st_size)).readline

def fastfrac(a,b,M):
    numb = pow(b,M-2,M)
    return ((a%M)*(numb%M))%M

def comb(n,k,M):
    if n<k: return 0
    if n==k: return 1
    if (n,k) in storecomb:  return storecomb[(n,k)]

    num1 = factor[n]
    num2 = factor[k]
    num3 = factor[n-k]
    num = (num2*num3)%M
    output = fastfrac(num1,num)
    storecomb[(n,k)] = output
    return output


def main(t):


    n,M = map(int,input().split())
    factor = [1]
    for i in range(1,n+1):  factor.append(factor[-1]*(i)%M)

    def fastfrac(a,b,M):


        numb = pow(b,M-2,M)
        return ((a%M)*(numb%M))%M

    comb = [[0 for _ in range(n+2)] for _ in range(n+1)]
    comb[0][0] = 1
 

    for i in range(1,n+1):
        for j in range(i+1):
            comb[i][j] = (comb[i-1][j-1] + comb[i-1][j])% M
     
         

 



    ans = 0
    for i in range(n//2,n-1):
        for j in range(n-1-i):
            num1 = comb[n-2-i][j]
            num2 = i-(i-n//2)*2
            num3 = factor[i+j-1]
            ans += (num1*num2)%M * num3 % M
            ans %= M

    if n%2==0:  ans += factor[n-2] 

        
        
        



    print(ans*n%M)




















T = 1 t = 1
while t<=T:
    main(t)
    t += 1

C++ Code:

#include<bits/stdc++.h>
using namespace std;
const int MAXN = 1e4;
int MOD;
int n,m,fac[MAXN + 5],inv[MAXN + 5];
int qpow(int a,int n){
    int ret = 1;
    while(n){
        if(n & 1)ret = 1ll * ret * a % MOD;
        a = 1ll * a * a % MOD;
        n >>= 1;
    }   
    return ret;
}
int c(int n,int m){
    if(m > n)return 0;
    return 1ll * fac[n] * inv[m] % MOD * inv[n - m] % MOD;
}
int main(){
    scanf("%d%d",&n,&MOD);
    fac[0] = 1;
    inv[0] = 1;
    for(int i = 1; i <= MAXN; i++){
        fac[i] = 1ll * fac[i - 1] * i % MOD;
        inv[i] = qpow(fac[i],MOD - 2);
    }
    long long ans = 0;
    for (int i = n / 2; i < n; i++){
        int x = n / 2 * 2 - i;
        for (int j = 0; j <= max(0, n - i - 2); j++){
            ans = (ans + 1ll * n * x % MOD * c(max(0, n - i - 2),j) % MOD * fac[i + j - 1] % MOD) % MOD;
        }
    }
    cout << ans;
}
		 				  	 		 	   		 	 	  					


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST